package net.oschina.renyong.query.generator;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.springframework.util.ResourceUtils;

import net.oschina.renyong.query.generator.po.ConfigViews;

/**
 * 封装对查询配置文件query_sql.xml的访问
 * @author Timothy
 */
public class QuerySqlXmlService {

	/**
	 * 解析获取特殊查询配置集ConfigViews
	 * @param resourceLocation 查询配置文件地址
	 * @return 特殊查询配置集ConfigViews
	 * @throws IOException IO异常
	 * @throws JAXBException JAXB异常
	 */
	public static ConfigViews getConfigViews(String resourceLocation) throws IOException, JAXBException {
		// File xmlFile = ResourceUtils.getFile("classpath:query_sql.xml");
		File xmlFile = ResourceUtils.getFile(resourceLocation);
		Reader reader = new FileReader(xmlFile);
		// String text = FileUtils.readFileToString(xmlFile, "UTF-8");
		// StringReader sr = new StringReader(text);
		JAXBContext context = JAXBContext.newInstance(ConfigViews.class);
		Unmarshaller unmarshaller = context.createUnmarshaller();
		ConfigViews configViews = (ConfigViews) unmarshaller.unmarshal(reader);
		return configViews;
	}

}
